<template>
  <div class="main">
    <el-card>
      <div class="grid-content bg-purple" v-if="LoginComponents">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="ruleForm.username"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              placeholder="请输入密码"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin">登录</el-button>
            <el-button @click="toRegister">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="grid-content bg-purple" v-else>
        <!-- 通过绑定事件返回true设置LoginComponents为true，显示登录页面 -->
        <RegisterView @register-success="LoginComponents = true"></RegisterView>
      </div>
    </el-card>
  </div>
</template>

<script>
import RegisterView from '@/views/user/components/RegisterView.vue';

export default {
  name: 'LoginView',
  data () {
    // 用户名规则
    const checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      } else {
        callback();
      }
    };

    // 密码规则
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };

    return {
      LoginComponents: true,
      ruleForm: {
        username: '',
        pass: '',
      },
      // 校验
      rules: {
        username: [
          { validator: checkUsername, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
      },
    };
  },
  methods: {
    handleLogin () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 处理登录逻辑//必须是到子路径，不能单写/n会导致堆栈错误
          this.$router.push('/n/home');
          // console.log('登录成功');
          // 调用后端 API 进行登录验证
          // 示例：this.loginAPI(this.ruleForm);
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    toRegister () {
      this.LoginComponents = !this.LoginComponents;
    }
  },
  components: {
    RegisterView
  }
};
</script>

<style scoped>
.main {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url("../../../assets/images/1.jpg") no-repeat center center;
  background-size: cover;
}

#main_bord {
  padding: 20px;
  border-radius: 8px;
  background-color: #ffffff;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease-in-out;
}

#main_bord:hover {
  transform: translateY(-5px);
}

.grid-content {
  padding: 20px;
  border-radius: 8px;
  background-color: #ffffff;
}

.demo-ruleForm {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  width: 100%;
  border: 1px solid #dcdcdc;
  border-radius: 4px;
  padding: 8px 12px;
  font-size: 14px;
  color: #333;
  outline: none;
  transition: border-color 0.3s;
}

.el-input:focus {
  border-color: #409eff;
}

.el-button {
  margin-right: 10px;
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s, color 0.3s;
}

.el-button[type="primary"] {
  background-color: #409eff;
  color: #ffffff;
}

.el-button[type="primary"]:hover {
  background-color: #66b1ff;
}

.el-button:active {
  transform: translateY(1px);
}

.el-button[disabled] {
  background-color: #f5f7fa;
  color: #c0c4cc;
  cursor: not-allowed;
}
</style>